package com.ehualu.getes.service;

import com.ehualu.getes.until.*;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.springframework.stereotype.Service;

import java.awt.geom.Point2D;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: 吴敬超
 * @Date: 2019/11/12 20:21
 */
@Service
public class EsServiceImpl implements EsService {


    private static String host = "37.158.97.130,37.158.97.131,37.158.97.132";
    private static int port = 24148;
    private static String schema = "http";


    @Override
    public int getnum(String host, int port, String schema) {
//        RestHighLevelClient client = EsUtil.getHighLevelClient(host, port, schema);
//
//        int num = EsUtil.getshij2("2019-11-02T09:45:23.000Z", "2019-11-02T09:50:23.000Z", "guoche_201911", "3715010111626", client);
//
//
//        EsUtil.close(client);
//
//
//        return num;
        return 0;
    }

    @Override
    public String gcsd(String index, String[] kkid1, String[] kkid2, int tstime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -8);

        cal.add(Calendar.MINUTE, tstime);
        Date stardate = cal.getTime();

        String starttime = simpleDateFormat.format(stardate);
        System.out.println("****starttime*****：" + starttime);

        Calendar calend = Calendar.getInstance();
        calend.add(Calendar.HOUR, -8);

//        calend.add(Calendar.MINUTE, -30);
        Date enddate = calend.getTime();

        String endtime = simpleDateFormat.format(enddate);
        System.out.println("****endtime*****：" + endtime);


        List<Double> gcsdlist = new ArrayList<>();


        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);


        ESsdUntil eSsdUntil = new ESsdUntil();

        List<String[]> kk1list = eSsdUntil.getgcjl(starttime, endtime, index, kkid1, client);

        List<String[]> kk2list = eSsdUntil.getgcjl(starttime, endtime, index, kkid2, client);

        esClientbui.close(client);


        if (kk1list.size() == 0) {

            System.out.println("卡口点位1近10分钟无过车");

            return "0";
        } else if (kk2list.size() == 0) {

            System.out.println("卡口点位2近10分钟无过车");

            return "0";

        }

        double kk1jd = Double.valueOf(kk1list.get(0)[2]);
        double kk1wd = Double.valueOf(kk1list.get(0)[3]);

        double kk2jd = Double.valueOf(kk2list.get(0)[2]);
        double kk2wd = Double.valueOf(kk2list.get(0)[3]);

        Point2D point1 = new Point2D.Double(kk1jd, kk1wd);

        Point2D point2 = new Point2D.Double(kk2jd, kk2wd);

//        两点间米数
        double dsize = Dissize.getDistance(point1, point2);
        System.out.println("两点间米数:" + dsize);

//        循环遍历卡口1的数据
        for (int i = 0; i < kk1list.size(); i++) {

//          卡口1的车牌
            String kk1cph = kk1list.get(i)[0];
//           卡口1过车时间
            String kk1gcsj = kk1list.get(i)[1];

//卡口2循环遍历
            for (int y = 0; y < kk2list.size(); y++) {
                String kk2cph = kk2list.get(y)[0];
                String kk2gcsj = kk2list.get(y)[1];


                if (kk2cph.equals(kk1cph)) {

                    System.out.println("找到相同的车牌:" + kk2cph);


                    Date firstdate = null;
                    Date lastdate = null;
                    try {
                        firstdate = simpleDateFormat.parse(kk1gcsj);
                        lastdate = simpleDateFormat.parse(kk2gcsj);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    long firstdatelong = firstdate.getTime();
                    long lastdatelong = lastdate.getTime();
                    long stimecz = ((firstdatelong - lastdatelong) / (1000));


                    System.out.println("两个时间之间的秒差为：" + stimecz);


                    long absstimecz = Math.abs(stimecz);

                    System.out.println("两个时间之间的秒差绝对值为：" + absstimecz);
                    double gcsdone = dsize / absstimecz;

                    System.out.println("当前过车速度：" + gcsdone);

                    gcsdlist.add(gcsdone);

                    break;
                }


            }


        }


        double sum = 0;
        for (int n = 0; n < gcsdlist.size(); n++) {

            sum = sum + gcsdlist.get(n);

        }


        int gclistsi = gcsdlist.size();


        if (gclistsi == 0) {
            System.out.println("前后两个卡口没有匹配到过车");
            return "0";
        }


        Double pjsd = sum / gclistsi;

        System.out.println("平均过车速度m/s：" + pjsd);

        Double pjsdkmh = pjsd * 3.6;

        System.out.println("平均过车速度km/h：" + pjsdkmh);

        String strpjsd = pjsdkmh.toString();
        return strpjsd;
    }


    @Override
    public String gcsj(String index, String[] kkid1, String[] kkid2, int tstime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -8);

        cal.add(Calendar.MINUTE, tstime);
        Date stardate = cal.getTime();

        String starttime = simpleDateFormat.format(stardate);
        System.out.println("****starttime*****：" + starttime);

        Calendar calend = Calendar.getInstance();
        calend.add(Calendar.HOUR, -8);

//        calend.add(Calendar.MINUTE, -30);
        Date enddate = calend.getTime();

        String endtime = simpleDateFormat.format(enddate);
        System.out.println("****endtime*****：" + endtime);


        List<Long> gcsdlist = new ArrayList<>();


        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);


        ESsdUntil eSsdUntil = new ESsdUntil();

        List<String[]> kk1list = eSsdUntil.getgcjl(starttime, endtime, index, kkid1, client);

        List<String[]> kk2list = eSsdUntil.getgcjl(starttime, endtime, index, kkid2, client);

        esClientbui.close(client);


        if (kk1list.size() == 0) {

            System.out.println("卡口点位1近10分钟无过车");

            return "0";
        } else if (kk2list.size() == 0) {

            System.out.println("卡口点位2近10分钟无过车");

            return "0";

        }


//        循环遍历卡口1的数据
        for (int i = 0; i < kk1list.size(); i++) {

//          卡口1的车牌
            String kk1cph = kk1list.get(i)[0];
//           卡口1过车时间
            String kk1gcsj = kk1list.get(i)[1];

//卡口2循环遍历
            for (int y = 0; y < kk2list.size(); y++) {
                String kk2cph = kk2list.get(y)[0];
                String kk2gcsj = kk2list.get(y)[1];


                if (kk2cph.equals(kk1cph)) {

                    System.out.println("找到相同的车牌:" + kk2cph);


                    Date firstdate = null;
                    Date lastdate = null;
                    try {
                        firstdate = simpleDateFormat.parse(kk1gcsj);
                        lastdate = simpleDateFormat.parse(kk2gcsj);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    long firstdatelong = firstdate.getTime();
                    long lastdatelong = lastdate.getTime();
                    long stimecz = ((firstdatelong - lastdatelong) / (1000));


                    System.out.println("两个时间之间的秒差为：" + stimecz);


                    Long absstimecz = Math.abs(stimecz);

                    System.out.println("两个时间之间的秒差绝对值为：" + absstimecz);

                    gcsdlist.add(absstimecz);

                    break;
                }


            }


        }


        int gclistsi = gcsdlist.size();


        if (gclistsi == 0) {
            System.out.println("前后两个卡口没有匹配到过车");
            return "0";
        }
        double sum = 0;


        for (int n = 0; n < gcsdlist.size(); n++) {

            sum = sum + gcsdlist.get(n);

        }


        Double pjsd = sum / gclistsi;

        System.out.println("平均过车时间单位秒：" + pjsd);


        String strpjsd = pjsd.toString();
        return strpjsd;
    }


    @Override
    public String getliulct(String index, String startime, String endtime, String dltype, String dlbh, String fx) {

        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);

        OracleUtills oracleUtills = new OracleUtills();

        List<String> listdldm = oracleUtills.getDldm(dltype, dlbh);


        int listdldmlong = listdldm.size();


        if (listdldmlong == 0) {
            System.out.println("kkxx备案表中没有匹配到合适道路代码");
            return "0";

        }

        String[] szdldm = listdldm.toArray(new String[listdldm.size()]);


        ESlluntil eSlluntil = new ESlluntil();

        String avgtj = eSlluntil.getliuluntilfordl(index, startime, endtime, fx, szdldm, client);

        esClientbui.close(client);

        return avgtj;
    }

    @Override
    public String getliulct7d(String index, String startime, String endtime, String dltype, String dlbh, String fx, List<String> listdldm) {


        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);


        int listdldmlong = listdldm.size();


        if (listdldmlong == 0) {
            System.out.println("kkxx备案表中没有匹配到合适道路代码");
            return "0";

        }

        String[] szdldm = listdldm.toArray(new String[listdldm.size()]);


        ESlluntil eSlluntil = new ESlluntil();

        String avgtj = eSlluntil.getliuluntilfordl(index, startime, endtime, fx, szdldm, client);

        esClientbui.close(client);

        return avgtj;
    }

    @Override
    public String getliulwhp(String index, String startime, String endtime, String dltype, String dlbh, String fx) {


        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);

        OracleUtills oracleUtills = new OracleUtills();

        List<String> listdldm = oracleUtills.getDldm(dltype, dlbh);


//        List<String> listwhpcp = oracleUtills.getwhpcp();
        List<String> listwhpcphc = oracleUtills.getwhpcphc();

        List<String> listwhpcppt = oracleUtills.getwhpcppt();

        int listdldmlong = listdldm.size();


        if (listdldmlong == 0) {
            System.out.println("kkxx备案表中没有匹配到合适道路代码");
            return "0";

        }

        String[] szdldm = listdldm.toArray(new String[listdldm.size()]);
        String[] szwhpcphc = listwhpcphc.toArray(new String[listwhpcphc.size()]);

        String[] szwhpcppt = listwhpcppt.toArray(new String[listwhpcppt.size()]);


        String whphcsz[] = new String[2];

        whphcsz[0] = "01";
        whphcsz[1] = "15";

        String whpptsz[] = new String[1];

        whpptsz[0] = "02";


        ESlluntil eSlluntil = new ESlluntil();

        Double avgtjhc = eSlluntil.getliuluntilforwhpxf(index, startime, endtime, fx, szdldm, szwhpcphc, whphcsz, client);
        Double avgtjpt = eSlluntil.getliuluntilforwhpxf(index, startime, endtime, fx, szdldm, szwhpcppt, whpptsz, client);

        esClientbui.close(client);

        Double avgsum = avgtjhc + avgtjpt;


        System.out.println("avgtjhc:" + avgtjhc);
        System.out.println("avgtjpt:" + avgtjpt);

        System.out.println("avgsum:" + avgsum);

        return avgsum.toString();

    }

    @Override
    public Double getliulwhp7d(String index, String startime, String endtime, String dltype, String dlbh, String fx, List<String> listdldm, String[] szwhpcp, String[] whpcplx) {

        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);


        int listdldmlong = listdldm.size();


        if (listdldmlong == 0) {
            System.out.println("kkxx备案表中没有匹配到合适道路代码");
            return 0.0;

        }

        String[] szdldm = listdldm.toArray(new String[listdldm.size()]);

        ESlluntil eSlluntil = new ESlluntil();

        Double avgtjhc = eSlluntil.getliuluntilforwhpxf(index, startime, endtime, fx, szdldm, szwhpcp, whpcplx, client);


        esClientbui.close(client);

        return avgtjhc;


    }

    @Override
    public Map<String, List<String>> getguacarnum(String carnumtype, String index, String guahoucarnum, Integer gcfw, Integer timcs) {

        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);

        OracleUtills oracleUtills = new OracleUtills();
        Fenworduntil fenworduntil = new Fenworduntil();

        ESguaUntil eSguaUntil = new ESguaUntil();

        TimeUntil timeUntil = new TimeUntil();
//        得到返回集合
        SearchHit[] searchHitstop = eSguaUntil.getguokkidtopSearchHit(index, guahoucarnum, gcfw, client);
        System.out.println("根据挂车车尾号牌得到前N条 条数：" + searchHitstop.length);

        List<String> flaglist = new ArrayList<>();

        List<Map<String, String>> listset = new ArrayList<>();

        Set<String> setcarnumall = new HashSet<>();

        String hpysstr = "1";

//        循环遍历集合
        for (SearchHit hit : searchHitstop) {

            Map<String, Object> sourceAsMap = hit.getSourceAsMap();

            String strkkid = (String) sourceAsMap.get("caddresscode");
            String strkkmc = (String) sourceAsMap.get("ccollectionaddress");
            String strgcsj = (String) sourceAsMap.get("createtime");
            String strjd = (String) sourceAsMap.get("jd");

            String strwd = (String) sourceAsMap.get("wd");


            System.out.println("strkkid:" + strkkid);
            System.out.println("strkkmc:" + strkkmc);
            System.out.println("strgcsj:" + strgcsj);

            System.out.println("strjd:" + strjd);

            System.out.println("strwd:" + strwd);

            flaglist.add(strkkid);

            /*
             *
             * 获得分词结果
             * */
            String strkkmclike = fenworduntil.fenciuntil(strkkmc);

            System.out.println("分词返回结果:" + strkkmclike);


//                向前推n秒
            String startime = timeUntil.timeturnuntil(strgcsj, -timcs);

            //                向后推n秒
            String endtime = timeUntil.timeturnuntil(strgcsj, timcs);


            List<String> kkbhlist = oracleUtills.getkkidlike(strkkid, strkkmclike, strjd, strwd);


            System.out.println("找到对应点位其他设备个数" + kkbhlist.size());


            if (kkbhlist.size() == 0) {

                System.out.println("找到对应点位其他设备个数为0 跳过");

                continue;
            }
            String[] kkbhsz = kkbhlist.toArray(new String[kkbhlist.size()]);

            System.out.println("************getcpfortimeandkkid start****************");
//                一堆 k  v
            Map<String, String> mapcp = eSguaUntil.getcpfortimeandkkid(carnumtype, startime, endtime, index, kkbhsz, hpysstr, client);
            System.out.println("************getcpfortimeandkkid end****************");

            System.out.println("对应点位过车过车数" + mapcp.size());


            Set<String> carnumset = mapcp.keySet();

//                所有的k 车牌
            setcarnumall.addAll(carnumset);

//                所有的  k  v
            listset.add(mapcp);

        }

        esClientbui.close(client);

        Map<String, List<String>> mapcar = new HashMap<>();

//        对所有找到的车牌循环遍历，计算重复次数
        for (String stranyonecarnum : setcarnumall) {

            Integer carnumcf = 0;

            List<String> listallinfo = new ArrayList<>();
            System.out.println("stranyonecarnum is:" + stranyonecarnum);

            for (Map<String, String> anyonedianwei : listset) {

                if (anyonedianwei.containsKey(stranyonecarnum)) {

                    String onepic = anyonedianwei.get(stranyonecarnum);
                    System.out.println("onepic is:" + onepic);

                    if (listallinfo.contains(onepic)) {
                        System.out.println("过车数据重复");

                    } else {

                        carnumcf = carnumcf + 1;

                        listallinfo.add(onepic);
                    }


                }


            }

            listallinfo.add(0, carnumcf.toString());

            mapcar.put(stranyonecarnum, listallinfo);

        }

        return mapcar;
    }

}
